package hesso.smartdraw.ch.guielements;

import hesso.smartdraw.ch.DrawWallScreen;
import hesso.smartdraw.ch.model.Color;

import java.util.ArrayList;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.widget.Toast;

public class LevelRemoveHandler extends Sprite {

	private Color color;

	private ArrayList<Level> levels;

	private Scene scene;

	private boolean isSub;

	private LevelAddHandler addHandler;

	private DrawWallScreen dw;

	public LevelRemoveHandler(float pX, float pY, ArrayList<Level> levels, boolean isSub, Scene scene, LevelAddHandler addHandler, TextureRegion minus,
			DrawWallScreen dw) {	

		super(pX, pY, minus);

		this.dw = dw;
		this.levels = levels;
		this.isSub = isSub;
		this.scene = scene;
		this.addHandler = addHandler;
		//setColor(Color.RED);
	}

	private void setColor(Color c) {

		this.color = c;
		super.setColor(c.getRed(), c.getGreen(), c.getBlue());
	}

	public void removeLevel() {

		if(levels.size()-1 >= 0) {

			Level tempLevel = levels.get(levels.size()-1);

			levels.remove(tempLevel);
			scene.detachChild(tempLevel);
			scene.unregisterTouchArea(tempLevel);

			if(!isSub) {
				this.setPosition(this.getX(), this.getY() + this.getHeight());
				this.addHandler.setPosition(addHandler.getX(), addHandler.getY() + this.getHeight());
			} else {

				this.setPosition(this.getX(), this.getY() - this.getHeight());
				this.addHandler.setPosition(addHandler.getX(), addHandler.getY() - this.getHeight());
			}
		}else {
			//Toast.makeText(DrawWallScreen.this, "Sorry your device does NOT support MultiTouch!\n\n(No PinchZoom is possible!)", Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
		switch(pSceneTouchEvent.getAction()) {
		case TouchEvent.ACTION_DOWN:

			break;

		case TouchEvent.ACTION_MOVE:

			break;
		case TouchEvent.ACTION_UP:
			AlertDialog.Builder builder = new AlertDialog.Builder(dw);
			String direction = (isSub)?"down":"up";
			builder.setMessage("Are you sure you want to delete this level? " + direction)
			.setCancelable(false)
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int id) {
					if(dw.deleteLevel(!isSub)){
						removeLevel();
					}else{
						Toast.makeText(dw, "Could not delete the Level.\n"+
								"- You can't delete your actual level.\n"+
								"- You can't delete the basement", Toast.LENGTH_LONG);
					}
					//dw.this.finish();
					//dialog.cancel();
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
			AlertDialog alert = builder.create();
			alert.show();
			//
			break;
		}
		return true;
	}

}
